﻿namespace HuaWei.DAI
{
    using System;
    using System.Data;

    public abstract class SqlDataTypeConvert
    {
        protected SqlDataTypeConvert()
        {
        }

        public static DbType GetDbType(Type type)
        {
            if (type == typeof(bool))
            {
                return DbType.Boolean;
            }
            if (type == typeof(byte))
            {
                return DbType.Byte;
            }
            if (type == typeof(byte[]))
            {
                return DbType.Binary;
            }
            if (type == typeof(DateTime))
            {
                return DbType.DateTime;
            }
            if (type == typeof(decimal))
            {
                return DbType.Decimal;
            }
            if (type == typeof(double))
            {
                return DbType.Double;
            }
            if (type == typeof(float))
            {
                return DbType.Single;
            }
            if (type == typeof(Guid))
            {
                return DbType.Guid;
            }
            if (type == typeof(short))
            {
                return DbType.Int16;
            }
            if (type == typeof(int))
            {
                return DbType.Int32;
            }
            if (type == typeof(long))
            {
                return DbType.Int64;
            }
            if (type == typeof(object))
            {
                return DbType.Object;
            }
            if (type == typeof(string))
            {
                return DbType.String;
            }
            if (type == typeof(TimeSpan))
            {
                return DbType.Time;
            }
            if (type == typeof(ushort))
            {
                return DbType.UInt16;
            }
            if (type == typeof(uint))
            {
                return DbType.UInt32;
            }
            if (type != typeof(ulong))
            {
                throw new Exception("类型" + type.ToString() + "暂不支持！");
            }
            return DbType.UInt64;
        }

        public static SqlDbType GetSqlDbType(DbType type)
        {
            switch (type)
            {
                case DbType.AnsiString:
                    return SqlDbType.VarChar;

                case DbType.Binary:
                    return SqlDbType.VarBinary;

                case DbType.Byte:
                    return SqlDbType.TinyInt;

                case DbType.Boolean:
                    return SqlDbType.Bit;

                case DbType.Currency:
                    return SqlDbType.Money;

                case DbType.DateTime:
                    return SqlDbType.DateTime;

                case DbType.Decimal:
                    return SqlDbType.Decimal;

                case DbType.Double:
                    return SqlDbType.Float;

                case DbType.Guid:
                    return SqlDbType.UniqueIdentifier;

                case DbType.Int16:
                    return SqlDbType.SmallInt;

                case DbType.Int32:
                    return SqlDbType.Int;

                case DbType.Int64:
                    return SqlDbType.BigInt;

                case DbType.Object:
                    return SqlDbType.Text;

                case DbType.Single:
                    return SqlDbType.Real;

                case DbType.String:
                    return SqlDbType.NVarChar;

                case DbType.AnsiStringFixedLength:
                    return SqlDbType.Char;

                case DbType.StringFixedLength:
                    return SqlDbType.NChar;
            }
            throw new Exception("类型 " + type.ToString() + " 暂不支持!");
        }

        public static string GetSqlDbTypeForCreateTable(DbType type)
        {
            switch (type)
            {
                case DbType.AnsiString:
                    return "VarChar(255)";

                case DbType.Binary:
                    return "VarBinary(100)";

                case DbType.Byte:
                    return "TinyInt";

                case DbType.Boolean:
                    return "Bit";

                case DbType.Currency:
                    return "Money";

                case DbType.DateTime:
                    return "DateTime";

                case DbType.Decimal:
                    return "Decimal(18,4)";

                case DbType.Double:
                    return "Float";

                case DbType.Guid:
                    return "UniqueIdentifier";

                case DbType.Int16:
                    return "SmallInt";

                case DbType.Int32:
                    return "Int";

                case DbType.Int64:
                    return "BigInt";

                case DbType.Object:
                    return "Text";

                case DbType.Single:
                    return "Real";

                case DbType.String:
                    return "NVarChar(255)";

                case DbType.AnsiStringFixedLength:
                    return "Char(10)";

                case DbType.StringFixedLength:
                    return "NChar(10)";
            }
            throw new Exception("类型 " + type.ToString() + " 暂不支持!");
        }

        public static SqlDbType Parse(string strSqlDbType)
        {
            switch (strSqlDbType)
            {
                case "BigInt":
                    return SqlDbType.BigInt;

                case "Binary":
                    return SqlDbType.Binary;

                case "Bit":
                    return SqlDbType.Bit;

                case "Char":
                    return SqlDbType.Char;

                case "DateTime":
                    return SqlDbType.DateTime;

                case "Decimal":
                    return SqlDbType.Decimal;

                case "Float":
                    return SqlDbType.Float;

                case "Image":
                    return SqlDbType.Image;

                case "Int":
                    return SqlDbType.Int;

                case "Money":
                    return SqlDbType.Money;

                case "NChar":
                    return SqlDbType.NChar;

                case "NText":
                    return SqlDbType.NText;

                case "NVarChar":
                    return SqlDbType.NVarChar;

                case "Real":
                    return SqlDbType.Real;

                case "SmallDateTime":
                    return SqlDbType.SmallDateTime;

                case "SmallInt":
                    return SqlDbType.SmallInt;

                case "SmallMoney":
                    return SqlDbType.SmallMoney;

                case "Text":
                    return SqlDbType.Text;

                case "Timestamp":
                    return SqlDbType.Timestamp;

                case "TinyInt":
                    return SqlDbType.TinyInt;

                case "UniqueIdentifier":
                    return SqlDbType.UniqueIdentifier;

                case "VarBinary":
                    return SqlDbType.VarBinary;

                case "VarChar":
                    return SqlDbType.VarChar;

                case "Variant":
                    return SqlDbType.Variant;
            }
            throw new Exception("类型 " + strSqlDbType + " 暂不支持!");
        }

        public static Type ParseToType(string strType)
        {
            Type type = typeof(int);
            switch (strType)
            {
                case "BigInt":
                    return typeof(long);

                case "Binary":
                    return typeof(byte[]);

                case "Bit":
                    return typeof(bool);

                case "Char":
                    return typeof(char);

                case "DateTime":
                    return typeof(DateTime);

                case "Decimal":
                    return typeof(decimal);

                case "Float":
                    return typeof(float);

                case "Image":
                    return typeof(byte[]);

                case "Int":
                    return typeof(int);

                case "Money":
                    return typeof(decimal);

                case "NChar":
                    return typeof(string);

                case "NText":
                    return typeof(string);

                case "NVarChar":
                    return typeof(string);

                case "Real":
                    return typeof(double);

                case "SmallDateTime":
                    return typeof(DateTime);

                case "SmallInt":
                    return typeof(short);

                case "SmallMoney":
                    return typeof(decimal);

                case "Text":
                    return typeof(string);

                case "Timestamp":
                    return typeof(TimeSpan);

                case "TinyInt":
                    return typeof(int);

                case "UniqueIdentifier":
                    return typeof(object);

                case "VarBinary":
                    return typeof(byte[]);

                case "VarChar":
                    return typeof(string);

                case "Variant":
                    return typeof(object);
            }
            throw new Exception("类型 " + strType + " 暂不支持!");
        }
    }
}

